package presentation;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import org.jfree.chart.plot.dial.ArcDialFrame;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;

public class PPercentMeter  extends DialPlot implements Observer {
	private static final long serialVersionUID = 1L;
	private DefaultValueDataset data;
	private int value = 0;
	private DecimalFormat df = new DecimalFormat("#");
	private DialTextAnnotation title;
	
	public PPercentMeter(String titleText, double[] boundary) {
		if (boundary[0] == 0 && boundary[1] == 0) {
			boundary[0] = 0;
			boundary[1] = 100;
		}
		
		Double steps = (boundary[1]-boundary[0])/5.;
		
		/** The dataset. */
		data = new DefaultValueDataset(value);
		this.setDataset(data);

		// Dial title
		title = new DialTextAnnotation(titleText + " %");
        title.setAngle(90);
        title.setFont(new Font("Arial", Font.BOLD, 25));
        title.setRadius(0.72);
        this.addLayer(title);
        
        // Tickmarks and text
        StandardDialScale scale = new StandardDialScale(boundary[0], 
        		boundary[1], 115, -50, steps.intValue(), 6);
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.07);
        scale.setTickLabelFont(new Font("Arial", Font.PLAIN, 14));
        scale.setTickLabelPaint(Color.black);
        scale.setTickLabelFormatter(new DecimalFormat("##"));
        this.addScale(0, scale);
		
		this.setView(0.21, 0.0, 0.58, 0.30);
		
        // Frame
		ArcDialFrame dialFrame = new ArcDialFrame(60.0, 60.0);
		dialFrame.setInnerRadius(0.60);
		dialFrame.setOuterRadius(0.90);
		
		dialFrame.setBackgroundPaint(Color.lightGray);
        dialFrame.setForegroundPaint(Color.darkGray);
        this.setDialFrame(dialFrame);
        
        // Neddle
        DialPointer needle = new DialPointer.Pin();
        needle.setRadius(0.85);
        this.addLayer(needle);
        
        // Background
        this.setBackground(new DialBackground(Color.white));
	}

	@Override
	public void update(Observable obs, Object arg) {
		if (arg instanceof Number) {
			data.setValue((Number) arg);
			title.setLabel(df.format(arg) + " %");
		}
	}
}